import { useEffect, useState } from 'react';
import { connect } from 'dva';
import { Table, Tag, Space, Button } from 'antd';
import { history } from 'umi';
function EmployeeList({ employee, dispatch }) {
  // componentDidMount
  useEffect(() => {
    // dispatch({
    //   type: 'employee/getAll',
    // });
  }, []);
  //const [list, setList] = useState([]);
  function handleDelete(id) {
    //console.log(id, 22);
    //使用state
    //setList(list.filter((e) => e.key !== id));
    //使用dva
    dispatch({
      type: 'employee/deleteEmployee',
      payload: {
        id,
      },
    });
  }

  function handleEdit(id) {
    history.push('/employee/' + id);
  }

  const { list } = employee;
  // const list = [
  //   {
  //     key: '1',
  //     firstName: 'John',
  //     lastName: 'Brown',
  //     age: 32,
  //     address: 'New York No. 1 Lake Park',
  //     tags: ['nice', 'developer'],
  //   },
  //   {
  //     key: '2',
  //     firstName: 'Jim',
  //     lastName: 'Green',
  //     age: 42,
  //     address: 'London No. 1 Lake Park',
  //     tags: ['loser'],
  //   },
  //   {
  //     key: '3',
  //     firstName: 'Joe',
  //     lastName: 'Black',
  //     age: 32,
  //     address: 'Sidney No. 1 Lake Park',
  //     tags: ['cool', 'teacher'],
  //   },
  // ];
  const columns = [
    {
      title: 'Name',
      children: [
        {
          title: 'First Name',
          dataIndex: 'firstName',
          key: 'firstName',
        },
        {
          title: 'Last Name',
          dataIndex: 'lastName',
          key: 'lastName',
        },
      ],
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
    {
      title: 'Tags',
      key: 'tags',
      dataIndex: 'tags',
      render: (tags) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: 'Action',
      key: 'action',
      render: (text, record) => (
        <Space size="middle">
          <a onClick={() => handleEdit(record.key)}>I/nvite {record.name}</a>
          <a onClick={() => handleDelete(record.key)}>Delete</a>
        </Space>
      ),
    },
  ];
  return (
    <div>
      {/* <h1>Employee List Page</h1>
      <ul>
        {employee.map((e) => (
          <li key={e.id}>
            {e.name}{' '}
            <button
              onClick={() =>
                dispatch({
                  type: 'employee/deleteEmployee',
                  payload: {
                    id: e.id,
                  },
                })
              }
            >
              删除
            </button>
          </li>
        ))}
      </ul> */}
      <Table dataSource={list} columns={columns} />
    </div>
  );
}

export default connect(({ employee }) => ({ employee }))(EmployeeList);
